import 'package:flutter/material.dart';
import 'package:ninetyfour/l10n/app_localizations.dart';
import '../../manger/global_config.dart';
import '../../models/yh_info_model.dart';
import '../../services/number_utils.dart';

class CouponListItem extends StatelessWidget {
  final CouponListModel coupon;
  final int index;
  final VoidCallback onUsePressed;

  const CouponListItem({
    super.key,
    required this.coupon,
    required this.index,
    required this.onUsePressed,
  });

 @override
Widget build(BuildContext context) {
  final localizations = AppLocalizations.of(context)!;

  return Container(
    margin: const EdgeInsets.only(bottom: 10, left: 15, right: 15),
    height: 170,
    child: Stack(
      children: [
        Material(
          borderRadius: BorderRadius.circular(12),
          color: Colors.white,
          child: Padding(
            padding: const EdgeInsets.only(top: 15, bottom: 15),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 类型标签和标题行
                Padding(
                  padding: const EdgeInsets.only(left: 15),
                  child: Row(
                    children: [
                      if (isChineseLocale())
                      Container(
                        width: 70,
                        height: 22,
                        decoration: BoxDecoration(
                          color: GlobalConfig.titleBlueColor,
                          borderRadius: BorderRadius.circular(11),
                        ),
                        alignment: Alignment.center,
                        child: Text(
                          coupon.scope,
                          style: const TextStyle(
                            color: Colors.white,
                            fontSize: 13,
                          ),
                        ),
                      ),
                      if (isChineseLocale())
                      const SizedBox(width: 10),
                      SizedBox(
                        width: 200,
                        child: Text(
                          coupon.name,
                          style: TextStyle(
                            color: GlobalConfig.label2Color,
                            fontSize: 15,
                          ),
                        ),
                      ),
                    ],
                  ),
                ),

                const SizedBox(height: 5),

                // 有效期和价格行
                Padding(
                  padding: const EdgeInsets.only(left: 15, right: 12),
                  child: Column(
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            isChineseLocale() ? '${localizations.claimTime}: ${coupon.createTime}' : coupon.effectiveTime,
                            style: TextStyle(
                              color: GlobalConfig.labelColor,
                              fontSize: 12,
                            ),
                          ),
                          Text(
                            
                            coupon.type == "1" ? NumberUtils.formatDecimalValue(coupon.parvalue * 10) : coupon.symbol + coupon.parvalue.toString(),
                            style: TextStyle(
                              color: GlobalConfig.titleRedColor,
                              fontSize: 22,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ],
                      ),
                      const SizedBox(height: 1),
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          if (isChineseLocale())
                          Text(
                            '${localizations.validTime}: ${coupon.effectiveTime}',
                            style: TextStyle(
                              color: GlobalConfig.labelColor,
                              fontSize: 12,
                            ),
                          ),
                          Text(
                            coupon.infor,
                            style: TextStyle(
                              color: GlobalConfig.titleRedColor,
                              fontSize: 12,
                              letterSpacing: -0.5,
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),

                // 分隔线
                Container(
                  margin: const EdgeInsets.only(top: 10, bottom: 5),
                  height: 10,
                  decoration: const BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage('assets/images/Dividing_line.png'),
                      fit: BoxFit.fitWidth,
                    ),
                  ),
                ),

                // 描述文字
                Padding(
                  padding: const EdgeInsets.only(left: 15, right: 100,top: 5), // 留出空间给按钮
                  child: Text(
                    localizations.instructions + coupon.description,
                    style: TextStyle(
                      color: GlobalConfig.label666Color,
                      fontSize: 12,
                    ),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ],
            ),
          ),
        ),

        // ✅ 状态按钮/图标：使用 Positioned 固定在右下角
        Positioned(
          right: 12,
          bottom: index == 0 ? 13 : -10,
          child: Builder(
            builder: (_) {
              if (index == 0) {
                return InkWell(
                  onTap: onUsePressed,
                  child: Container(
                    width: 80,
                    height: 32,
                    decoration: BoxDecoration(
                      color: GlobalConfig.titleBlueColor,
                      borderRadius: BorderRadius.circular(17),
                    ),
                    alignment: Alignment.center,
                    child: Text(
                      localizations.useNow,
                      style: const TextStyle(
                        color: Colors.white,
                        fontSize: 14,
                      ),
                    ),
                  ),
                );
              } else if (index == 1) {
                return Image.asset(isChineseLocale() ? 'assets/images/已使用.png' : 'assets/images/已使用en.png', width: 80, height: 80);
              } else {
                return Image.asset(isChineseLocale() ? 'assets/images/已过期.png' : 'assets/images/已过期en.png', width: 80, height: 80);
              }
            },
          ),
        ),
      ],
    ),
  );
}

}